$(function () {
    var queryUrl = '../moduleerror/list';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '?Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '模具号', name: 'moduleNo', index: 'MODULE_NO', width: 80},
            // {label: '模具编码', name: 'moduleCode', index: 'MODULE_CODE', width: 80},
            {label: '模具类型', name: 'moduleType', index: 'MODULE_TYPE', width: 80},
            {
                label: '是否关闭', name: 'isOff', index: 'IS_OFF', width: 80, formatter: function (value) {
                    return sysEnums.YES_OR_NO[value];
                }
            },
            // {
            //     label: '是否更换备件',
            //     name: 'isReplaceSparepart',
            //     index: 'IS_REPLACE_SPAREPART',
            //     width: 80,
            //     formatter: function (value, col, row) {
            //         var html = value === 0 ? '否' : "<button class='btn btn-outline btn-info' onclick='vm.goModuleReplacePart(\"" + row.id + "\")'>更换记录</button>";
            //         return html;
            //     }
            // },
            {
                label: '维护日期', name: 'maintainTime', index: 'MAINTAIN_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '车间', name: 'workshopName', index: 'workshopName', width: 80},
            { name: 'workshop', index: 'WORKSHOP', width: 80,hidden: true},
            /*{label: '生产线', name: 'productLine', index: 'PRODUCT_LINE', width: 80},*/
           // {label: '现象描述', name: 'description', index: 'DESCRIPTION', width: 80},
            //{label: '处理建议', name: 'dealSuggest', index: 'DEAL_SUGGEST', width: 80},
           // {label: '处理意见', name: 'dealOpinion', index: 'DEAL_OPINION', width: 80},
            {label: '模具类型', name: 'moduleType', index: 'MODULE_TYPE', width: 80},
            // {label: '产品系列', name: 'productSeries', index: 'PRODUCT_SERIES', width: 80},
            {label: '现象描述', name: 'description', index: 'description', width: 80,hidden: true},
            {label: '处理方法', name: 'dealOpinion', index: 'dealOpinion', width: 80,hidden: true},
            {label: '备注信息', name: 'dealSuggest', index: 'dealSuggest', width: 80,hidden: true},




            {label: '异常复核确认人',hidden: true, name: 'reviewUserName', index: 'reviewUserName', width: 80},
            {label: '异常消除状态',hidden: true, name: 'errorDisStatus', index: 'ERROR_DIS_STATUS', width: 80, formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: '异常审核时间',hidden: true, name: 'errorCheckTime', index: 'ERROR_CHECK_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '操作人姓名',hidden: true, name: 'createUserName', index: 'createUserName', width: 80},
            {
                label: '操作时间',hidden: true, name: 'createTime', index: 'CREATE_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },

            //{label: '所属维护记录', name: 'maintainRecord', index: 'MAINTAIN_RECORD', width: 80},
            {
                label: '异常审核详情', name: 'devCode', index: 'DEV_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            /*{label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80},
            {label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleError: {
            updateTime: '',
            updateUser: '',
            createTime: '',
            createUser: '',
            delFlag: '',
            errorCheckTime: '',
            errorDisStatus: '',
            errorReviewUser: '',
            maintainRecord: '',
            productType: '',
            productSeries: '',
            moduleType: '',
            dealOpinion: '',
            dealSuggest: '',
            description: '',
            productLine: '',
            workshop: '',
            maintainTime: '',
            isReplaceSparepart: '',
            isOff: '',
            moduleCode: '',
            id: '',
        },
        ruleValidate: {
            moduleCode: [
                {required: true, message: '模具编码不能为空', trigger: 'blur'}
            ]
        },
        q: {
            moduleCode: ''
        }
    },
    methods: {
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if (!data.reviewUserName) {
                data.reviewUserName = "-"
            }
            if (!data.createUserName) {
                data.createUserName = "-"
            }
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>异常复核确认人：</td><td>" + data.reviewUserName + "</td></tr>" +
                "<tr><td>异常消除状态：</td><td>" + sysEnums.CHECK_FLAG[data.errorDisStatus] + "</td></tr>" +
                "<tr><td>异常审核时间：</td><td>" + transDate(data.errorCheckTime, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>操作人：</td><td>" + data.createUserName + "</td></tr>" +
                "<tr><td>操作时间：</td><td>" + transDate(data.createTime, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>现象描述：</td><td>" + data.description + "</td></tr>" +
                "<tr><td>处理方法：</td><td>" + data.dealOpinion + "</td></tr>" +
                "<tr><td>备注信息：</td><td>" + data.dealSuggest + "</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleError = {
                updateTime: '',
                updateUser: '',
                createTime: '',
                createUser: '',
                delFlag: 1,
                errorCheckTime: '',
                errorDisStatus: 0,
                errorReviewUser: '',
                maintainRecord: '',
                productType: '',
                productSeries: '',
                moduleType: '',
                dealOpinion: '',
                dealSuggest: '',
                description: '',
                productLine: '',
                workshop: '',
                maintainTime: new Date(),
                isReplaceSparepart: 1,
                isOff: 1,
                moduleCode: '',
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.moduleError.id == (null || '') ? "../moduleerror/save" : "../moduleerror/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleError),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../moduleerror/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../moduleerror/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleError = r.moduleError;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'moduleCode': vm.q.moduleCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                moduleCode: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        review: function () { //审核
            var result = getSelectedRowData("#jqGrid");
            var id = result.id;
            if (id == null) {
                return;
            }
            if (result.errorDisStatus === "未审核") {
                vm.getInfo(id);
                confirm('确定要审核选中的记录？', function () {
                    vm.moduleError.errorDisStatus = 1;
                    Ajax.request({
                        url: '../moduleerror/review',
                        params: JSON.stringify(vm.moduleError),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function (r) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            } else {
                alert('本记录已审核!');
            }
        },
        goModuleReplacePart: function (id) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-pencil"></i> 更换记录',
                content: '../module/modulereplacepart.html?id=' + id + '&modulCode='
            })
        }
    }
});
